/**
 * Created by cuppi on 2017/7/7.
 */
let express = require('express');
let router = express.Router();
let FaceManager = require('../ai/FaceManager');
let path = require('path');
let fs = require('fs');
// data base
let UserFaceDAO = require('../util/UserFaceDAO');

var NetworkManager = require('../network/NetworkManager');
/* GET users listing. */
router.get('/', function (req, res, next) {
    UserFaceDAO.getUserFaceList().then(list => {
        console.log(list);
        res.json({data: list, errorCode: 0, message: ''});
        res.end();
    }, error => {
        console.log(error)
        res.send(error);
    })
    // UserFaceDAO.addUserFace({userId: 898, userGroup: 'develop', userFaceUrl:'hehe'}).then(result => {
    //     res.json({data: result, errorCode: 0, message: ''});
    //     res.end();
    // }, error => {
    //     res.json({data: null, errorCode: -1, message: error.message});
    //     res.end();
    // });

});

router.post('/', function (req, res, next) {
    let userId = req.query.userId;
    let userGroup = req.query.userGroup;
    let userFaceUrl = req.query.userFaceUrl;
    if (req.body.userId){
        userId = req.body.userId;
    }
    if (req.body.userGroup){
        userGroup = req.body.userGroup;
    }
    if (req.body.userFaceUrl){
        userFaceUrl = req.body.userFaceUrl;
    }
    if (!userId || !userGroup || !userFaceUrl){
        res.json({data: {}, errorCode: -1, message: '参数缺失'});
        return;
    }

    UserFaceDAO.addUserFace({userId, userGroup, userFaceUrl}).then(result => {
        res.json({data: result, errorCode: 0, message: ''});
        res.end();
    }, error => {
        res.json({data: null, errorCode: -1, message: error.message});
        res.end();
    });

    // let id = req.query.id;
    // let name = req.query.name;
    // let faceUrlList = req.query.faceUrlList;
    // if (req.body.id){
    //     id = req.body.id;
    // }
    //
    // if (req.body.name){
    //     name = req.body.name;
    // }
    //
    // if (req.body['faceUrlList[]']){
    //     faceUrlList = req.body['faceUrlList[]'];
    //     if (typeof(faceUrlList) === 'string'){
    //         faceUrlList = [req.body['faceUrlList[]']];
    //     }
    // }
    // console.log(id);
    // console.log(name);
    // console.log(faceUrlList);
    // if (!faceUrlList || !id || !name){
    //     res.json({data: {}, errorCode: -1, message: '参数缺失'});
    //     return;
    // }
    //
    // FaceManager.defaultManager().registerUserMutapleFace(id, name, faceUrlList).then(data => {
    //     console.log(data);
    //     res.json({data: data, errorCode: 0, message: ''});
    //     res.end();
    // }, error => {
    //     console.log(error);
    //     res.send(error);
    // });
});

module.exports = router;